package com.billing.persistance.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.beans.factory.annotation.Autowired;

import com.billing.persistance.dao.IGenericDAO;

/**
 * @author Victor
 */
@Repository
public class GenericDaoHibernateImpl<T, PK extends Serializable> implements IGenericDAO<T, PK>
{
    @Autowired
    private SessionFactory sessionFactory;
   
    private Class<T> type;

    public GenericDaoHibernateImpl()
    {
    }

    @SuppressWarnings("unchecked")
	public PK create(T o)
    {
        return (PK) sessionFactory.getCurrentSession().save(o);
    }

    @SuppressWarnings("unchecked")
	public T read(PK id)
    {
        return (T) sessionFactory.getCurrentSession().get(getType(), id);
    }

    public void update(T o)
    {
    	sessionFactory.getCurrentSession().update(o);
    }

    public void delete(T o)
    {
    	sessionFactory.getCurrentSession().delete(o);
    }

    
    public List<T> getList(String whereClaure) {
    	return sessionFactory.getCurrentSession().find("from " + type.getName() + " x");
    }
    
    // Getters and setters
	public Class<T> getType() {
		return type;
	}

	public void setType(Class<T> type) {
		this.type = type;
	}

}   